package cn.hrlly.tao2shou.view.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.presenter.PublishCommodityPresenter;
import cn.hrlly.tao2shou.presenter.PublishCommodityPresenterImpl;
import cn.hrlly.tao2shou.view.view_interface.PublishCommodityView;

public class PublishFragment extends Fragment implements PublishCommodityView {

    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.etCommodityName)
    EditText etCommodityName;
    @Bind(R.id.etPrice)
    EditText etPrice;
    @Bind(R.id.etLocation)
    EditText etLocation;
    @Bind(R.id.etTime)
    EditText etTime;
    @Bind(R.id.etCommodityCount)
    EditText etCommodityCount;
    @Bind(R.id.spnClassification)
    Spinner spnClassification;
    @Bind(R.id.ratingBar)
    RatingBar ratingBar;
    @Bind(R.id.etCommodityDescription)
    EditText etCommodityDescription;
    @Bind(R.id.etBarterDescription)
    EditText etBarterDescription;
    @Bind(R.id.btnConfirm)
    Button btnConfirm;
    private PublishCommodityPresenter publishCommodityPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_publish, container, false);
        //绑定控件
        ButterKnife.bind(this, rootView);
        //初始化
        publishCommodityPresenter = new PublishCommodityPresenterImpl(this);
        return rootView;
    }

    //btnConfirm点击事件
    @OnClick(R.id.btnConfirm)
    public void confirm() {
        try {

            if (!etTime.getText().toString().equals("") && !etPrice.getText().toString().equals("") && !etLocation.getText().toString().equals("") && !etCommodityName.getText().toString().equals("") && !etCommodityCount.getText().toString().equals("") && !etCommodityDescription.getText().toString().equals("")) {
                publishCommodityPresenter.publishCommodity();
            } else {
                showMessage(R.string.fill_incomplete);
            }

        } catch (Exception e) {
            showMessage(R.string.information_to_fill_in_the_error);
        }
    }

    //以下是对fragment的操作
    @Override
    public File[] getCommodityPhotos() {
        return new File[0];
    }

    @Override
    public String getCommodityName() {
        return etCommodityName.getText().toString();
    }

    @Override
    public int getCommodityCount() {
        return Integer.parseInt(etCommodityCount.getText().toString());
    }

    @Override
    public String getClassification() {
        return spnClassification.toString();
    }

    @Override
    public double getExpectedPrice() {
        return Double.parseDouble(etPrice.getText().toString());
    }

    @Override
    public String getBarterDescription() {
        return etBarterDescription.getText().toString();
    }

    @Override
    public int getNewDegree() {
        return ratingBar.getNumStars();
    }

    @Override
    public String getCommodityDescription() {
        return etCommodityDescription.getText().toString();
    }

    @Override
    public String getExpectedTime() {
        return etTime.getText().toString();
    }

    @Override
    public String getExpectedLocation() {
        return etLocation.getText().toString();
    }

    @Override
    public void clearAll() {
        etBarterDescription.setText("");
        etCommodityCount.setText("");
        etCommodityDescription.setText("");
        etCommodityName.setText("");
        etLocation.setText("");
        etPrice.setText("");
        etTime.setText("");
        //这里还需要将viewpager中的图片清除
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }
}
